package com.niit.controller;


import com.niit.entity.Movie;
import com.niit.entity.User;
import com.niit.service.MovieService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Resource
    private MovieService MovieService;

    /**
     * 查询所有电影信息
     * @param Movie 电影查询条件
     * @return 电影列表信息
     */

    @RequestMapping("/findAllMovies")
    public List<Movie> findAllMovies() {
        return MovieService.findAllMovies();
    }

    @GetMapping("/findMovieBy/{id}")
    public Movie queryById(@PathVariable("id") Integer id) {
        return this.MovieService.findMovieById(id);
    }

    @RequestMapping("/findMovieByName")
    public Movie findMovieByCinema(@RequestBody Movie movie) {
        System.out.println("前端发送的数据:"+movie);
        String movie_name = movie.getMovieName();
        Movie  dbMovie  = this.MovieService.findMovieByName(movie_name);
        if (dbMovie != null) {
            return dbMovie;
        }
        return dbMovie;
    }

    @PostMapping("/addMovie")
    public Movie addMovie(@RequestBody Movie movie) {
        System.out.println("前端发送的数据:"+movie);
        return this.MovieService.addMovie(movie);
    }

    @RequestMapping("/updateMovie")
    public String updateMovie(@RequestBody Movie movie) {
        System.out.println("前端发送的数据:"+movie);
        int result = MovieService.updateMovie(movie);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    /*@RequestMapping("/deleteMovie/{id}")*/
    @RequestMapping("/deleteMovie")
    public String deleteMovie(@RequestBody Movie movie) {
        System.out.println("前端发送的数据:"+movie);
        Integer id = movie.getMovieId();
        System.out.println("前端发送数据id:"+id);
        Movie dbMovieID = this.MovieService.findMovieById(id);
        System.out.println("数据库获取的id:"+dbMovieID);
        if (dbMovieID == null) {
            // 处理id为null的情况，比如返回错误信息或者重定向到一个错误页面
            return "请求的ID不存在";
        }
        int result = MovieService.deleteMovie(dbMovieID.getMovieId());
        if(result >= 1 )
        {
            return "删除成功";
        }
        else
        {
            return "删除失败";
        }
    }







}
